<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Post
 *
 * @author neozilon
 */
class Nosql_Util_Post {

    /**
     * convert the post title into a friendly url part
     * changes all spaces for "-" and delete all non characters
     *
     * @param string $title
     * @return string
     */
    public static function cleanTitleForUrl($title) {
        $newString = str_replace(' ', '-', trim($title));
        $newString = preg_replace('/[^A-Za-z0-9-]/', '', strtolower($newString));
        return $newString;
    }

    /**
     * get an array of tags from a text
     * splitting the tags by the separator ","
     * @param string $text
     * @return string[]
     */
    public static function getArrayTagsFromText($text) {
        $tagsArray = array();
        $text = trim($text);
        if (strlen($text) > 0) {
            $splitted = preg_split("/,/", $text);
            foreach ($splitted as $tag) {
                $tagsArray[] = trim($tag);
            }
        }
        return $tagsArray;
    }

    /**
     * format the array of Tags and return a String glue by the second
     * parameter
     * @param array('tagid','tag') $tagsArray
     * @param string $glue
     * @return string
     */
    public static function formatTagsArray($tagsArray, $glue = ',') {

        $tags = '';
        if (count($tagsArray) > 0) {
            $tmp = array();
            foreach ($tagsArray as $tag) {
                $tmp[] = $tag['tag'];
            }

            $tags = implode(",", $tmp);
        }
        return $tags;
    }

}